package com.mz.mattress.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mz.mattress.admin.api.req.CouponSendRecordQueryReq;
import com.mz.mattress.admin.mapper.CouponMapper;
import com.mz.mattress.admin.mapper.CouponSendRecordMapper;
import com.mz.mattress.admin.service.CouponSendRecordService;
import com.mz.mattress.api.common.Result;
import com.mz.mattress.api.entity.Coupon;
import com.mz.mattress.api.entity.CouponSendRecord;
import com.mz.mattress.api.enums.CouponSendTypeEnum;
import com.mz.mattress.api.util.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class CouponSendRecordServiceImpl implements CouponSendRecordService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponSendRecordMapper couponSendRecordMapper;

    @Override
    public Result<IPage<CouponSendRecord>> listPage(CouponSendRecordQueryReq req) {

        IPage<CouponSendRecord> listPage = couponSendRecordMapper.listPage(req.newPage(), req.getMobile(), req.getStatus(), req.getCouponId(), req.getUserId(), req.getUserName(),
                req.getStartTime(), req.getEndTime());

        return Result.ok(listPage);
    }

    @Override
    public Result<CouponSendRecord> getById(Integer id) {
        return null;
    }

    @Override
    public Result addBySendType(String mobile, CouponSendTypeEnum enums) {

        if (StringTools.isEmpty(mobile)) {
            return Result.fail();
        }

        List<Coupon> coupons = couponMapper.selectList(new LambdaQueryWrapper<Coupon>().eq(Coupon::getSendType, enums.getValue()).eq(Coupon::getStatus, 1));

        for (Coupon coupon : coupons) {
            CouponSendRecord record = new CouponSendRecord();
            record.setCouponId(coupon.getId());
            record.setMobile(mobile);
            record.setCount(coupon.getCount());
            record.setStatus(0);
            couponSendRecordMapper.insert(record);
        }

        return Result.ok();
    }
}
